package work.droit.gui.swing;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.wb.swt.layout.grouplayout.GroupLayout;
import org.eclipse.wb.swt.layout.grouplayout.LayoutStyle;

import work.droit.data.Definition;
import work.droit.data.Fiche;
import work.droit.data.Jurisprudence;
import work.droit.data.Question;
import work.droit.gui.ApiController;
import work.droit.gui.ApiController.SearchParameters;
import work.util.data.table.Tables.Champ.SqlType;
import work.util.editor.EditorLauncher;

public class RechercheFrame extends Shell
{
	private Tree	tree;
	private Text	text;
	private Button	btnDfinitions;
	private Button	btnQuestions;
	private Button	btnJusrisprudences;
	private Button	btnFiches;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[])
	{
		try
		{
			Display display = Display.getDefault();
			RechercheFrame shell = new RechercheFrame(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed())
			{
				if (!display.readAndDispatch())
				{
					display.sleep();
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void launch()
	{
		Display display = getDisplay();
		this.open();
		this.layout();
		while (!this.isDisposed())
		{
			if (!display.readAndDispatch())
			{
				display.sleep();
			}
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public RechercheFrame(final Display display)
	{
		super(display, SWT.SHELL_TRIM);
		setBackground(ApiController.bgColor);
		setImage(SWTResourceManager.getImage(RechercheFrame.class, "/work/droit/rc/images/network_16.png"));

		Group grpDfinition = new Group(this, SWT.NONE);
		grpDfinition.setText("Recherche");
		FontData fontData = grpDfinition.getFont().getFontData()[0];
		fontData = grpDfinition.getFont().getFontData()[0];
		Font font = new Font(display, new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC));
		grpDfinition.setFont(font);

		Group group = new Group(this, SWT.NONE);

		Group grpParamtres = new Group(this, SWT.NONE);
		grpParamtres.setText("Param\u00E8tres");
		grpParamtres.setFont(font);

		final Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.setText("Lancer la recherche");
		btnNewButton.setEnabled(false);
		btnNewButton.addSelectionListener(new SelectionAdapter()
		{
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				String searchPattern = text.getText();
				SearchParameters searchParameters = new SearchParameters();
				searchParameters.fiches = btnFiches.getSelection();
				searchParameters.jurisprudence = btnJusrisprudences.getSelection();
				searchParameters.definitions = btnDfinitions.getSelection();
				searchParameters.questions = btnQuestions.getSelection();

				ApiController.fillTreeSearch(tree, searchPattern, searchParameters);
			}
		});

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(GroupLayout.TRAILING)
						.add(groupLayout
								.createSequentialGroup()
								.addContainerGap()
								.add(groupLayout
										.createParallelGroup(GroupLayout.TRAILING)
										.add(GroupLayout.LEADING, group, GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
										.add(GroupLayout.LEADING, grpParamtres, GroupLayout.DEFAULT_SIZE, 412,
												Short.MAX_VALUE)
										.add(GroupLayout.LEADING, grpDfinition, GroupLayout.DEFAULT_SIZE, 412,
												Short.MAX_VALUE)
										.add(GroupLayout.LEADING, btnNewButton, GroupLayout.DEFAULT_SIZE, 412,
												Short.MAX_VALUE)).addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(GroupLayout.LEADING).add(
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.add(grpDfinition, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.RELATED)
						.add(grpParamtres, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(btnNewButton)
						.addPreferredGap(LayoutStyle.UNRELATED)
						.add(group, GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE).addContainerGap()));

		btnFiches = new Button(grpParamtres, SWT.CHECK);
		btnFiches.setBounds(10, 18, 66, 16);
		btnFiches.setText("Fiches");
		btnFiches.setSelection(true);

		btnJusrisprudences = new Button(grpParamtres, SWT.CHECK);
		btnJusrisprudences.setBounds(82, 18, 93, 16);
		btnJusrisprudences.setText("Jurisprudences");
		btnJusrisprudences.setSelection(true);

		btnQuestions = new Button(grpParamtres, SWT.CHECK);
		btnQuestions.setBounds(197, 18, 93, 16);
		btnQuestions.setText("Questions");
		btnQuestions.setSelection(true);

		btnDfinitions = new Button(grpParamtres, SWT.CHECK);
		btnDfinitions.setBounds(296, 18, 93, 16);
		btnDfinitions.setText("D\u00E9finitions");
		btnDfinitions.setSelection(true);

		group.setLayout(new FillLayout(SWT.HORIZONTAL));

		tree = new Tree(group, SWT.BORDER);
		MouseListener mouseListenerQuestion = new MouseListener()
		{
			@Override
			public void mouseUp(MouseEvent e)
			{
			}

			@Override
			public void mouseDown(MouseEvent e)
			{
			}

			@Override
			public void mouseDoubleClick(MouseEvent e)
			{
				Tree tree = (Tree) e.getSource();
				TreeItem[] selec = tree.getSelection();
				if (selec != null && selec.length == 1)
				{
					TreeItem t = selec[0];
					Object obj = t.getData();
					if (obj instanceof Fiche)
					{
						Fiche fiche = (Fiche) obj;
						Number number = (Number) fiche.getValue(Fiche.id);
						long l = number.longValue();
						String name = (String) fiche.getValue(Fiche.name);
						EditorLauncher.launchViewer( String.valueOf(l) + "f", name, "Fiche");
					}
					if (obj instanceof Jurisprudence)
					{
						Jurisprudence jurisprudence = (Jurisprudence) obj;
						Number number = (Number) jurisprudence.getValue(Jurisprudence.id);
						long l = number.longValue();
						String name = (String) jurisprudence.getValue(Jurisprudence.name);
						EditorLauncher.launchViewer( String.valueOf(l) + "j", name, "Jurisprudence");
					}
					if (obj instanceof Definition)
					{
						Definition definition = (Definition) obj;
						DefinitionFrame definitionFrame = new DefinitionFrame(display);
						definitionFrame.launch(definition, true);

					}
					if (obj instanceof Question)
					{
						Question question = (Question) obj;
						QuestionFrame questionFrame = new QuestionFrame(display);
						questionFrame.launch(question, false);
					}
				}
			}
		};
		tree.addMouseListener(mouseListenerQuestion);

		FillLayout fl_grpDfinition = new FillLayout(SWT.HORIZONTAL);
		fl_grpDfinition.marginHeight = 10;
		grpDfinition.setLayout(fl_grpDfinition);

		text = new Text(grpDfinition, SWT.BORDER);
		text.setTextLimit(SqlType.VARCHAR100.size());
		text.addKeyListener(new KeyListener()
		{
			@Override
			public void keyReleased(KeyEvent e)
			{
				if (text.getText().length() >= 1)
					btnNewButton.setEnabled(true);
				else
					btnNewButton.setEnabled(false);
			}

			@Override
			public void keyPressed(KeyEvent e)
			{
			}
		});

		setLayout(groupLayout);

		createContents();
	}
	
	@Override
	public void close()
	{
		ApiController.clearSearchCache();
		super.close();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents()
	{
		setText("SWT Application");
		setSize(450, 477);
	}

	@Override
	protected void checkSubclass()
	{
		// Disable the check that prevents subclassing of SWT components
	}
}
